//
//  TCLYPasswordViewController.m
//  YuErmuYing
//
//  Created by LYang on 16/8/27.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "TCLYPasswordViewController.h"
#import "Macros.h"
#import "TCLYMineModel.h"
#import "TCLYRegisterView.h"
#import "TCLYUser.h"
@interface TCLYPasswordViewController ()
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@property (nonatomic, strong)TCLYMineModel * mineModel;
@property (nonatomic, strong)TCLYRegisterView * registerView;
@property (nonatomic, strong)TCLYUser * user;
@end

@implementation TCLYPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加触碰手势(为了可以隐藏键盘)
    [self createTitleView];
    UITapGestureRecognizer * textFeildTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:textFeildTap];
    self.view.backgroundColor = bgColor;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatePasswordSucceed) name:@"updatePasswordSucceed" object:nil];
    self.mineModel = [[TCLYMineModel alloc] init];
    self.user = [[TCLYUser alloc] init];
    self.registerView = [[TCLYRegisterView alloc] init];
    
    [self createBtn];
    // Do any additional setup after loading the view from its nib.
}

-(void)createTitleView{
    self.navigationController.navigationBar.barStyle = UIBarMetricsCompactPrompt;
    self.navigationController.navigationBar.backgroundColor = statusColor;
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, screenWidth, 20)];
    view.backgroundColor = navColor;
    [self.navigationController.navigationBar addSubview:view];
    
    UILabel * titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"修改密码";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.frame = CGRectMake(0, 0, 50, 20);
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = titleLabel;
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(12, 22, 20, 20);
    [leftBtn setImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
    leftBtn.tag = 91;
    [leftBtn addTarget:self action:@selector(statusBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
}
-(void)statusBtnAction:(id)sender{
    switch (((UIButton *)sender).tag) {
        case 91:{
            [self.navigationController popViewControllerAnimated:YES];
            self.navigationController.navigationBarHidden = NO;
            self.tabBarController.tabBar.hidden = YES;
        }
            break;
        case 92:{
            
        }
            break;
    }
}
-(void) createBtn{
    self.submitBtn.backgroundColor = btnColor;
    [self.submitBtn setTitle:@"保存" forState:UIControlStateNormal];
    [self.submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.submitBtn.layer.cornerRadius = 5;
    self.submitBtn.layer.masksToBounds = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//隐藏键盘的方法
-(void)dismissKeyboard{
    for (int j = 0; j<3; j++) {
        UITextField * textField = [self.view viewWithTag:501+j];
        [textField resignFirstResponder];
    }
}
-(IBAction)submitBtnAction:(id)sender{
    UITextField * oldPwd = [self.view viewWithTag:501];
    UITextField * newPwd = [self.view viewWithTag:502];
    UITextField * new1Pwd = [self.view viewWithTag:503];
    if (![newPwd.text isEqualToString:new1Pwd.text]) {
        [self.registerView prompt:@"两次密码不一致" Controller:self];
        return;
    }
    NSLog(@"%@",oldPwd.text);
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:oldPwd.text,@"pass",self.user.userId,@"user_id", new1Pwd.text, @"password", nil];
    [self.mineModel updatePasswordParameter:dic];
}

-(void)updatePasswordSucceed{
    NSLog(@"%@",self.mineModel.updatePasswordSucceedStr);
    if ([self.mineModel.updatePasswordSucceedStr isEqual:@200]) {
        self.navigationController.navigationBarHidden = YES;
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else if ([self.mineModel.updatePasswordSucceedStr isEqual:@404]){
        [self.registerView prompt:@"修改密码失败" Controller:self];
    }else if ([self.mineModel.updatePasswordSucceedStr isEqual:@400]){
        [self.registerView prompt:@"输入的原始密码不正确" Controller:self];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
